package com.bone.eventsweb.controller;
import cn.hutool.core.util.StrUtil;
import com.bone.eventsweb.entity.UserEntity;
import com.bone.eventsweb.service.UserService;
import com.bone.eventsweb.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/eventsweb/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Map<String,String> login(@RequestBody UserEntity user) {
        HashMap<String, String> map = new HashMap<>();

        //判断用户名和密码是否为空
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            map.put("msg","数据输入的不合法");
        }
        UserEntity us = userService.login(user);
        //登录成功
        map.put("code","200");
        map.put("username",us.getUsername());
        map.put("password",us.getPassword());
        map.put("token",us.getToken());
        return map;
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserEntity user){
        return userService.register(user);
    }
}
